//
//  CustomLinkButton.m
//  iSeal5200
//
//  Created by Yuan.He on 12-12-5.
//  Copyright (c) 2012年 http://www.heyuan110.com. All rights reserved.
//

#import "CustomLinkButton.h"

@implementation CustomUnderLineLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(context, 0.0f, self.frame.size.height + 0.0f);
    CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height + 0.0f);
    CGContextSetStrokeColorWithColor(context, self.textColor.CGColor);
    CGContextStrokePath(context);
}
@end

@interface CustomLinkButton()
{
    @private
    CustomUnderLineLabel *titleLbl;
}
@property(nonatomic,retain) CustomUnderLineLabel *titleLbl;
@end

@interface CustomLinkButton(Private)

-(void)setContolHighlighted:(BOOL)highlighted;

@end

@implementation CustomLinkButton
@synthesize titleLbl;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        titleLbl = [[CustomUnderLineLabel alloc]initWithFrame:CGRectZero];
        [titleLbl setBackgroundColor:[UIColor clearColor]];
        [titleLbl setFont:FONT_NORMAL];
        [titleLbl setTextColor:[UIColor darkGrayColor]];
        [self addSubview:titleLbl];
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

-(void)setTitle:(NSString *)title_
{
    titleLbl.text = title_;
    [titleLbl sizeToFit];//自动计算出label的size
    CGRect selfFrame = self.frame;
    selfFrame.size = titleLbl.frame.size;
    self.frame = selfFrame;
    titleLbl.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
}

-(NSString *)title
{
    return titleLbl.text;
}

-(void)setContolHighlighted:(BOOL)highlighted
{
    titleLbl.textColor = highlighted?[UIColor blueColor]:[UIColor darkGrayColor];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self setContolHighlighted:YES];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    [self setContolHighlighted:YES];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    [super touchesEnded:touches withEvent:event];
    [self setContolHighlighted:NO];

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    [self setContolHighlighted:NO];
}

@end











